package m5_topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPassword("admin");
        f.setUsername("admin");
        Channel c=f.newConnection().createChannel();
        c.exchangeDeclare("topic_losg", BuiltinExchangeType.TOPIC);

        String queue= UUID.randomUUID().toString();
        c.queueDeclare(queue,false,true,true,null);
        System.out.println("输入绑定建,用空格隔开:");
        String s=new Scanner(System.in).nextLine();
        String[] a=s.split("//s+");
        for (String key: a) {
            c.queueBind(queue,"topic_losg",key);
        }

        DeliverCallback callback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody(), "UTF-8");
                String routingKey = message.getEnvelope().getRoutingKey();
                System.out.println("收到: "+routingKey+" - "+msg);
            }
        };

        //消费者取消时的回调对象
        CancelCallback cancel = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };
        c.basicConsume(queue,true,callback,cancel);

    }

}
